home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / fd / recvfile.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  693b  |  31 lines

  1. /*
  2.  * Receive a file descriptor from another process.
  3.  * Return the file descriptor if OK, otherwise return -1.
  4.  */
  5.  
  6. #include    <sys/types.h>
  7. #include    <sys/socket.h>
  8. #include    <sys/uio.h>
  9.  
  10. int
  11. recvfile(sockfd)
  12. int    sockfd;        /* UNIX domain socket to receive descriptor on */
  13. {
  14.     int        fd;
  15.     struct iovec    iov[1];
  16.     struct msghdr    msg;
  17.  
  18.     iov[0].iov_base = (char *) 0;        /* no data to receive */
  19.     iov[0].iov_len  = 0;
  20.     msg.msg_iov          = iov;
  21.     msg.msg_iovlen       = 1;
  22.     msg.msg_name         = (caddr_t) 0;
  23.     msg.msg_accrights    = (caddr_t) &fd;    /* address of descriptor */
  24.     msg.msg_accrightslen = sizeof(fd);    /* receive 1 descriptor */
  25.  
  26.     if (recvmsg(sockfd, &msg, 0) < 0)
  27.         return(-1);
  28.  
  29.     return(fd);
  30. }
  31.